package com.example.myapplication.tack_06;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_06.adapter.BooksAdapter;
import com.example.myapplication.tack_06.data.BooksListData;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;

public class Books extends AppCompatActivity {
    private ImageView exit;
    private int id = 1;
    private List<BooksListData.RowsDTO> list;
    private RecyclerView recyclerView;
    private TextView upper;
    private TextView below;
    private TextView ym;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_06__books);
        init();
        exit_();
        upper_();
        below_();

        getBooks(id);
    }

    private void below_() {

        below.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(id == 10){
                    below.setVisibility(View.GONE);
                }else{
                    upper.setVisibility(View.VISIBLE);
                    id++;
                    if(id == 10){
                        below.setVisibility(View.GONE);
                    }
                    ym.setText(id + "/10");
                    getBooks(id);
                }
            }
        });
    }

    private void upper_() {

        upper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(id == 1){
                    upper.setVisibility(View.GONE);
                }else{
                    below.setVisibility(View.VISIBLE);
                    id--;
                    if(id == 1){
                        upper.setVisibility(View.GONE);
                    }
                    ym.setText(id + "/10");
                    getBooks(id);
                }

            }
        });
    }

    private void getBooks(int id) {
        String url = "prod-api/api/digital-library/library/list?pageNum="+id+"+&pageSize=5";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BooksListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BooksListData booksListData = (BooksListData) obj;
                list = booksListData.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(id == 1){
                            upper.setVisibility(View.GONE);
                        }
                        booksAdapter();
                    }
                });
            }



            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void booksAdapter() {
        BooksAdapter booksAdapter = new BooksAdapter(list, Books.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Books.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(booksAdapter);

    }
    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Books.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        recyclerView = findViewById(R.id.list);
        upper = findViewById(R.id.upper);
        below = findViewById(R.id.below);
        ym = findViewById(R.id.ym);
    }
}